<?php
/***************************************************************************
 *   copyright : (C) 2009 Udrea Cristian
 *   site : http://code.google.com/p/testauction-php
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as 
 *   published by the Free Software Foundation; either version 2 of the
 *   License, or (at your option) any later version. Although none of the
 *   code may be sold. If you have been sold this script, get a refund.
 ***************************************************************************/
define('APP_ROOT', '../../');
//###############################################
include(APP_ROOT . 'include/session.php');
include('../library/functions.php');

if(!$session->logged_in && !$session->isAdmin()) {
	$session->logout();
	header('Location: index.php');
}

$action = isset($_GET['action']) ? $_GET['action'] : '';
if (isset($_GET['auctionId']) && (int)$_GET['auctionId'] > 0) {
	$auctionId = (int)$_GET['auctionId'];
} else {
	header('Location: index.php');
}
$delType = isset($_GET['delType']) ? $_GET['delType'] : 'error';

switch ($action) {
	case 'resumeAuction' :
		resumeAuction();
		break;

	case 'suspendAuction' :
		suspendAuction();
		break;

	case 'deleteAuction' :
		deleteAuction();
		break;
	
	case 'renewAuction' :
		renewAuction();
		break;

	default :
		// if action is not defined or unknown
		// move to main product page
		header('Location: index.php');
}

function resumeAuction() {
	global $auctionId;
	$sql = "UPDATE " . TBL_AUCTIONS . " SET suspended = 'N'";
	execute_update($sql);
	header("Location: index.php");	
}

function suspendAuction() {
	global $auctionId;
	$sql = "UPDATE " . TBL_AUCTIONS . " SET suspended = 'Y'";
	execute_update($sql);
	header("Location: index.php");	
}

function deleteAuction() {
	global $auctionId, $delType;
	// remove any references to this product from
	// tbl_order_item and tbl_cart
	execute_update('DELETE FROM '. TBL_AUCTIONS .' WHERE auction_id = ?', array($auctionId));
	// in the case of an error or open auction fully restore
	// all of the bidder's used credits if the auction is 
	// closed only restore the winner's credits
	$result = execute_query('SELECT userkey FROM '. TBL_OFFERS .' WHERE auction_id = ? ORDER BY timestamp DESC', array($auctionId));
	
	switch($delType) {
		case 'error' :
		case 'open' :
			foreach($result as $item) {
				execute_update('UPDATE '. TBL_CREDIT .' SET credit_balance = credit_balance + 1 WHERE userkey = ?', array($item['userkey']));
			}
			break;

		case 'closed' :
			$userkey = $result[0]['userkey'];
			$result = execute_query('SELECT * FROM '. TBL_OFFERS .' WHERE auction_id = ? AND userkey = ? ORDER BY timestamp DESC', array($auctionId, $userkey));
			$credits = count($result);
			execute_update('UPDATE '. TBL_CREDIT .' SET credit_balance = credit_balance + ? WHERE userkey = ?', array($credits, $userkey));
			break;

		default:
			break;
	}
	execute_update('DELETE FROM '. TBL_OFFERS .' WHERE auction_id = ?', array($auctionId));
	header('Location: index.php');
}

function renewAuction() {
	global $auctionId;
	$dates = execute_query('SELECT starts_timestamp FROM '.TBL_AUCTIONS.' WHERE closed = 0 ORDER BY starts_timestamp DESC');
	if(count($dates) > 0) {
		$nextstart = getNextBestDate($dates[0]['starts_timestamp']);
	}
	else {
		$nextstart = getNextBestDate(time());
	}
	execute_update('UPDATE ' . TBL_AUCTIONS . ' SET closed = 0, closed_timestamp = 0, time_left = time_interval, 
		starts_timestamp = ? WHERE auction_id = ?', array($nextstart, $auctionId));
	header('Location: index.php');
}
?>
